"""主页统计数据接口实现"""
from django.utils import timezone
from rest_framework.views import APIView
from rest_framework.response import Response
from users.models import User

# 统计用户总量
class UserTotalCountView(APIView):

    def get(self, request):
        # 1、统计用户总数量
        count = User.objects.count()
        date = timezone.localtime().date()

        # 2、构建响应数据
        return Response({
            'count': count,
            'date': date
        })


def get_local_0_time():
    """
    return local zero time
    :return: datetime
    """
    # 2020-7-13 09：53：50 Asia/Shanghai
    cur_time = timezone.localtime()
    # 取得零时刻
    local_0_time = cur_time.replace(hour=0, minute=0, second=0)
    return local_0_time

# 日增用户量
class UserDayCountView(APIView):

    def get(self, request):
        # 1、获取当日的零时 2020-7-13 0:0:0
        local_0_time = get_local_0_time()
        # 2、过滤今日新建的用户
        count = User.objects.filter(
            date_joined__gte=local_0_time
        ).count()
        # 3、构建响应数据返回
        return Response({
            'count': count,
            'date': local_0_time.date()
        })

# 日活跃用户量
class UserActiveCountView(APIView):

    def get(self, request):
        local_0_time = get_local_0_time()
        count = User.objects.filter(
            last_login__gte=local_0_time
        ).count()
        return Response({
            'count': count,
            'date': local_0_time.date()
        })


from orders.models import OrderInfo
# 日下单用户量统计
class UserOrderCountView(APIView):

    def get(self, request):
        # 已知条件是：今日的零时刻 —— 是订单表(从)的已知条件
        # 目标数据：  用户数量    —— 是用户表(主)的目标数据
        # 总结：根据从表的已知条件，查询主表数据

        local_0_time = get_local_0_time()
        # # 方案一：从从表入手查询
        #
        # # 过滤出今日下的所有的订单
        # orders = OrderInfo.objects.filter(
        #     create_time__gte=local_0_time
        # )
        # # 从订单对象中获取用户
        # user_set = set()
        # for order in orders:
        #     # order是每一个订单对象
        #     user_set.add(order.user) # 会自动去重
        # count = len(user_set)
        #
        # return Response({
        #     'count': count,
        #     'date': local_0_time.date()
        # })

        # 方案二：从主表入手查询
        user_queryset = User.objects.filter(
            orders__create_time__gte=local_0_time
        )
        count = len(set(user_queryset))

        return Response({
            'count': count,
            'date': local_0_time.date()
        })

from datetime import timedelta
class UserMonthCountView(APIView):

    def get(self, request):
        # 2020-7-13 0:0:0
        local_0_time = get_local_0_time()

        # 启始日期的零时刻： 启始日期 = 当日日期  - (总天数 - 1)
        start_0_time = local_0_time - timedelta(days=29)

        ret_list = []
        for index in range(30):
            # 1 -->  start_0_time + 0天        index: 0
            # 2 -->  start_0_time + 1天   index: 1
            # 3 -->  start_0_time + 2天   index: 2
            # .....
            # 30 --> start_0_time + 29天  index: 29
            calc_0_time = start_0_time + timedelta(days=index)
            next_0_time = calc_0_time + timedelta(days=1)
            # 由于统计的是过去的某一天，所有需要： 某一天的零时 <= 新建日期 < 次日的零时
            count = User.objects.filter(
                date_joined__gte=calc_0_time,
                date_joined__lt=next_0_time
            ).count()
            ret_list.append({
                'count': count,
                'date': calc_0_time.date()
            })

        return Response(ret_list)



# 日分类商品访问量
from rest_framework.generics import ListAPIView
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.home_serializers import *
class GoodsDayView(ListAPIView):
    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitCountModelSerializer

    def get_queryset(self):
        # 功能：获取序列化的目标数据
        local_0_time = get_local_0_time()
        return self.queryset.filter(
            create_time__gte=local_0_time
        )










